(function () {
    var arr = [];
    var html = "";
    var over = 4, count, now = 0;
    $.ajax({
        url: "http://chst.vip:1234/api/getinlanddiscount",
        type: "Get",
        data: {

        },
        success(suc) {
            arr = suc.result;
            for (now; now < over; now++) {
                if (now >= count) {
                    now = count;
                    over = count;
                    break;
                }
                html += `
                <div productid=${arr[now].productId}  class="productItem">
                    ${arr[now].productImg}
                    <h4>${arr[now].productName}</h4>
                    <b>${arr[now].productPrice}</b>
                    <p>${arr[now].productFrom}|${arr[now].productTime}</p>
                </div>
                `
            }
            $("main").html(html)
            $("main").on("click", ".productItem", function () {
                let productid = Number($(this).attr("productid"));
                location.href = `../pages/discount.html?productid=${productid}`
            })
        }
    })
    getData();
    function getData() {
        $.ajax({
            url: "http://chst.vip:1234/api/getinlanddiscount",
            type: "Get",
            data: {

            },
            success(suc) {
                arr = suc.result;
                loadlazy();
            }
        })
    }

    function loadData() {
        for (now; now < over; now++) {
            if(now>19){
                return 
            }
            if (now >= count) {
                now = count;
                over = count;
                break;
            }
            console.log(now);
            html += `
            <div productid=${arr[now].productId}   class="productItem">
                ${arr[now].productImg}
                <h4>${arr[now].productName}</h4>
                <b>${arr[now].productPrice}</b>
                <p>${arr[now].productFrom}|${arr[now].productTime}</p>
            </div>
            `
        }
        $("main").html(html)
    }
    // 懒加载
    function loadlazy() {
        var flag = true;
        $("main").scroll(function () {
            var clientHeight = $("main").height();
            var scrollHeight = $("main")[0].scrollHeight;
            var topHeight = $("main").scrollTop();
            var bottomHeight = scrollHeight - topHeight - clientHeight;
            if (bottomHeight < clientHeight) {
                if (flag) {
                    flag = false;
                    over += 4
                    loadData();
                }
            } else {
                flag = true;
            }
        })
    }


})()